home *** CD-ROM | disk | FTP | other *** search
- /* OPTLC/CCC - March 5, 1984 - jwk
- peephole optimizer for LC /ASM files
- */
- #include stdio/csh
- #define YES 1
- #define NO 0
- #option INLIB
- #define HOME 28 /* home cursor */
- #define CURDN 0x1A /* move cursor down one line*/
- #define CURRT 0x19 /* move cursor one char to right */
- int Inct, Otct;
- char *S1, *S2, *S3, *Tcp;
-
- main()
- { char *pshhl, *ldhl, *popde, *exde, *gwmac, *clgint;
- int flag;
- fprintf(stderr,"%c%c",28,31);
- fprintf(stderr,"OPTLC - Optimizes LC /ASM files.");
- lcack(1984);
- Inct=Otct=0;
- S1=alloc(256);
- S2=alloc(256);
- S3=alloc(256);
- pshhl="\tPUSH\tHL\n";
- ldhl="\tLD\tHL,";
- gwmac="\t$GETW\t";
- popde="\tPOP\tDE\n";
- exde="\tEX\tDE,HL\n";
- clgint="\tCALL\t@GINT\n";
-
- flag=inplin(S1);
- while (flag) {
- brktst();
- if (ucjr(S1)) { /* delete unreachable code */
- outlin(S1);
- *S1 = '\0';
- flag=inplin(S1);
- while ((*S1 == '\t') & (*(S1+1) != '$')) {
- flag=inplin(S1);
- if (!flag) break;
- }
- }
- if (strcmp(S1,pshhl)) { /*not equal*/
- outlin(S1);
- *S1 = '\0';
- flag=inplin(S1);
- }
- else { /* was PUSH HL */
- if ((flag=inplin(S2)) &&
- (strfind(S2,ldhl,0)>=0 ||
- strfind(S2,gwmac,0)>=0)) {
- /* was LD HL, */
- if ((flag=inplin(S3)) &&
- strcmp(S3,popde)==0) {
- /* was POP DE */
- outlin(exde);
- outlin(S2);
- *S1 = '\0';
- *S2 = '\0';
- flag=inplin(S1);
- }
- else { /* not POP DE */
- outlin(S1);
- outlin(S2);
- strcpy(S1,S3);
- *S2 = '\0';
- *S3 = '\0';
- }
- }
- else { /* not LD HL, */
- outlin(S1);
- strcpy(S1,S2);
- *S2 = '\0';
- }
- }
- }
- outlin(S1);
- exit (0);
- }
-
- inplin(x) /* read one line into buffer x */
- char *x;
- { int c, flag;
- char *xx;
- flag=TRUE;
- if (x==NULL) x=alloc(256);
- xx=x;
- while ((c=getchar()) != EOF) {
- *x++ = c;
- if (c=='\n') {
- *x = '\0';
- ++Inct;
- report();
- if (*xx == ';')
- flag=inplin(xx);
- return (flag);
- }
- }
- return (FALSE);
- }
-
- outlin(x)
- char *x;
- { if (x) {
- if (*x) ++Otct;
- while (*x) /* write to STDOUT */
- putchar(*x++);
- report();
- }
- }
- brktst() /* hold up or abort */
- { int c;
- if ((c=inkey())==0) return;
- if (c==1) 0x440d();
- if (c=='`') {
- while (inkey() != ' ')
- ;
- }
- return;
- }
-
- report()
- { setcur(0,6);
- fprintf(stderr,"Read %6d lines; ",Inct);
- fprintf(stderr,"wrote %6d\n",Otct);
- }
-
- ucjr(s) char *s; /* return YES if JP or RET */
- { int temp;
- temp=NO;
- if (strfind(s,"\tRET",0) >= 0)
- temp=YES;
- else if (strfind(s,"\tJP\t",0) >= 0 &&
- strfind(s,",",0) < 0)
- temp=YES;
- return(temp);
- }
-
- /* set cursor -- 02/11/84 addition.
- * this version should work with any screen size
- * since it always addresses from HOME position!
- */
- setcur(x,y)
- int x,y;
- { putc(HOME,stderr); /* home the cursor for reference */
- while (y--)
- putc(CURDN,stderr);/* move down to the line */
- while (x--)
- putc(CURRT,stderr);/* then over to the column */
- }
-
- #include lc/ack
-